
import 'dart:math';

import 'package:flutter/material.dart';

import 'data.dart';
import 'global.dart';

class ViewPage extends StatefulWidget{
  @override
  _ViewPageState createState() => _ViewPageState();

}

class _ViewPageState extends State<ViewPage>{
  int personCount=3;
  WordPair wp;
  String commonWord;
  String dintWord;
  int dintIndex;
  List<Person> persons;

  _ViewPageState(){
    init();
  }

  void init(){
    wp=Global.genPair();
    personCount=Global.personCount;
    Random random=Random();
    commonWord=wp.word1;
    dintWord=wp.word2;
    if(random.nextInt(2)==1){
      commonWord=wp.word2;
      dintWord=wp.word1;
    }
    dintIndex=random.nextInt(personCount);
    persons=[];
    for(int i=0;i<personCount;i++){
      if(i == dintIndex){
        Person p=Person(dintWord);
        p.isDint=true;
        persons.add(p);
      }else{
        persons.add(Person(commonWord));
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    //下划线widget预定义以供复用。
    Widget divider1=Divider(color: Colors.blue,);
    Widget divider2=Divider(color: Colors.green);
    var lv= ListView.separated(
      itemCount: Global.personCount,
      //列表项构造器
      itemBuilder: (BuildContext context, int index) {
        return Padding(
          //上下各添加8像素补白
          padding: const EdgeInsets.symmetric(vertical: 8.0),
          child: FlatButton(
            color: Colors.blue,
            disabledColor: Colors.grey,
            highlightColor: Colors.blue[700],
            colorBrightness: Brightness.dark,
            splashColor: Colors.grey,
            child: Text(getViewText(index)),
            onPressed: persons[index].isView ? null : () {
              showViewDialog(index).then((value) => {
                setViewed(index)
              });
            },
          ),
        );
      },
      //分割器构造器
      separatorBuilder: (BuildContext context, int index) {
        return index%2==0?divider1:divider2;
      },
    );
    return Scaffold(
      appBar: AppBar(
        title: Text("查看页"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Expanded(
              child: lv,
            ),
          ],
        ),
      ),

    );
  }

  Future<bool> showViewDialog(index) {
    return showDialog<bool>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text("提示"),
          content: Text(getWord(index)),
          actions: <Widget>[
            FlatButton(
              child: Text("确定"),
              onPressed: () {
                //关闭对话框并返回true
                Navigator.of(context).pop(true);
              },
            ),
          ],
        );
      },
    );
  }

  String getWord(int index){
    return persons[index].word;
  }

  String getViewText(int index){
    String vt="玩家 "+(index+1).toString();

    return vt;
  }

  bool isAllView(){
    bool allView=true;
    for(Person p in persons){
      if( ! p.isView){
        allView=false;
        break;
      }
    }
    return allView;
  }

  void setViewed(int index){
    setState(() {
      persons[index].isView=true;
    });
    if(isAllView()){
      Navigator.pushNamed(context, "/game", arguments: persons);
    }
  }
}